package com.network.web.receipt;

import com.network.web.Client.WebClient;
import com.network.web.Client.WebResponseBody;
import com.network.web.api.ReceiptApi;
import com.network.web.config.ReceiptConfig;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 28162
 */
@Slf4j
@Service
public class ReceiptService {
    @Resource
    private ReceiptConfig receiptConfig;

    @Resource
    private WebClient webClient;

    public WebResponseBody requestReceiptInfo() {
        ReceiptApi receiptApi = webClient.buildApiWithCertificate(
                receiptConfig.getUrl(),
                receiptConfig.getApiClass(),
                receiptConfig.getCertificatePath(),
                receiptConfig.getCertificatePassword(),
                receiptConfig.getKeyStoreType(),
                receiptConfig.getSslContextType()
        );

        RequestBody requestBody = RequestBody.create(
                MediaType.get(receiptConfig.getMediaType()),
                receiptConfig.getRequestHeader()
        );

        return webClient.processResponse(receiptApi.requestReceiptInfo(requestBody), receiptConfig.getUrl());
    }
}
